package com.lanhai.system;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import com.lanhai.Main;


/**
 * 监听UDP端口
 * @author liangmin
 *
 */
public class Accept4Agv implements Runnable {
	
	private DatagramPacket dp;
	private DatagramSocket ds;
	private int port;
	private Main main;
	
	public Accept4Agv(Main main, int port){
		this.port = port;
		this.main = main;
	}
	
	public void run() {
		byte buf[] = new byte[8];
		dp = new DatagramPacket(buf, buf.length);		
		try{
			ds = new DatagramSocket(port);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("已经运行了一个实例，退出!");
			System.exit(0);
		}
		while(true){
			try{
				ds.receive(dp);		
				main.handle(buf, dp.getAddress().getHostAddress());
			}catch(Exception e){
				e.printStackTrace();
			}
		}	
	
	}
}
